using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Drawing;

namespace Chess
{
    public abstract class ControlSuper
    {
        protected BanCo bc;
        protected Hashtable dsQC = new Hashtable();
        protected int _laCo = -1;
        protected Point _ktBanCo = new Point();
        protected int _ktOCo = 0;
        protected int _soOCo = 0;
        protected int _csHinhNext = 0;
        protected int qcDuocPhepDi = -1;
        protected bool _thTren = false;

        public virtual void KhoiTao(int qcDiTruoc)
        {
        }

        public int LaLoaiCo
        {
            get { return _laCo; }
        }

        public bool TheHienTren
        {
            get { return _thTren; }
        }

        public int CSHinhNext
        {
            get { return _csHinhNext; }
        }

        public int KickThuocOCo
        {
            get { return _ktOCo; }
        }

        public int SoOCo
        {
            get { return _soOCo; }
        }

        public Point KichThuocBanCo
        {
            get { return _ktBanCo; }
        }

        public Hashtable DSQuanCo
        {
            get { return dsQC; }
        }

        public int[] CPUDiCo()
        {
            return bc.CPUDiCo();
        }

        public bool KiemTraThang()
        {
            return bc.Kiem_Tra_Thang_Thua();
        }

        public virtual Hashtable[] DiMotNuocCo(int vtDau, int vtCuoi, int loaiPhong)
        {
            return null;
        }

        public virtual Hashtable[] DiMotNuocCo(int vtDau, int vtCuoi)
        {
            return null;
        }

        public Hashtable[] CacNuocDiCothe(int vtQuanCo)
        {
            if (!dsQC.Contains(vtQuanCo))
                return null;
            int qc = (int)dsQC[vtQuanCo];
            if (qcDuocPhepDi != LaQuan(qc))
                return null;
            AL_Int dsND = bc.CacNuocDiCoThe(vtQuanCo);
            if (dsND == null)
                return null;

            Hashtable[] kq = new Hashtable[3];
            kq[0] = new Hashtable();
            kq[1] = new Hashtable();
            kq[2] = new Hashtable();
            kq[2].Add(vtQuanCo, dsQC[vtQuanCo]);

            AL_Int temp = new AL_Int();
            for (int i = 0; i < dsND.Count - 2; ++i)
            {
                if (dsQC.Contains(dsND[i]))
                {
                    kq[1].Add(dsND[i], dsQC[dsND[i]]);
                }
                temp.Add(dsND[i]);
            }

            kq[1].Add("dsNC", temp);

            foreach (int each in dsQC.Keys)
            {
                if (!kq[1].Contains(each) && each != vtQuanCo)
                    kq[0].Add(each, dsQC[each]);
            }

            return kq;
        }

        public int ConvertXY2VT(int x, int y)
        {
            return (x * _soOCo + y);
        }

        protected int LaQuan(int qc)
        {
            return bc.LaQuan(qc);
        }

        public int LaVua(int qc)
        {
            return bc.LaVua(qc);
        }

        public byte[] XuatTrangThaiHienTai()
        {
            byte[] kq = bc.XuatBanCo();

            kq[0] = (byte)qcDuocPhepDi;

            return kq;
        }

        public virtual void LoadTrangThai(byte[] dl)
        {

        }

        public Hashtable[] Undo1NuocCo()
        {
            int[] kqUndo = bc.Undo1NuocCo();
            if (kqUndo == null)
                return null;

            Hashtable[] kq = new Hashtable[3];
            kq[0] = new Hashtable();
            kq[1] = new Hashtable();
            kq[2] = new Hashtable();

            dsQC.Remove(kqUndo[3]);
            if (kqUndo[4] != 0)
                dsQC.Add(kqUndo[3], kqUndo[4]);

            foreach (int each in dsQC.Keys)
            {
                kq[0].Add(each, dsQC[each]);
            }
            dsQC.Add(kqUndo[0], kqUndo[1]);

            kq[1].Add(0, kqUndo[3]);
            kq[1].Add(1, kqUndo[0]);
            kq[1].Add(2, kqUndo[1]);

            qcDuocPhepDi = (qcDuocPhepDi == 0) ? 1 : 0;

            return kq;
        }

        public int SoUndoToiDa()
        {
            return bc.SoUndoToiDa();
        }
    }
}
